package com.shopping.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.shopping.po.Stock;
import com.shopping.service.StockService;

@Controller
public class StockController {
	@Autowired
	private StockService stockService;
	@RequestMapping("/allStock")
	public String selectStock(Model model) {
		List<Stock> stock3 = stockService.findStockByAll();
		model.addAttribute("stock3",stock3);
		return "adminSuccess";
	}
	
	
	@RequestMapping("/selectGoods")
	public String selectgoods(Model model,HttpServletRequest request) {
		String status="1";
		List<Stock> stock = stockService.findGoodsByStatus(status);
		model.addAttribute("stock",stock);
		return "indexAfter";
	}
	//网页登录，并同时显示商品清单
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String selectgoods1(Model model,HttpServletRequest request) {
		String status="1";
		List<Stock> stock1 = stockService.findGoodsByStatus(status);
		model.addAttribute("stock1",stock1);
		return "index";
	}
	
	@RequestMapping("/updateStatus")
	public String updataStatus(Integer id,String status,HttpServletRequest request) {
		System.out.println(id);
		Integer stock_id = id;
		if(status.equals("1")) {
			status = "2";
			stockService.updateGoodsByStatus(stock_id,status);
		}else{
			status = "1";
		stockService.updateGoodsByStatus(stock_id,status);
		}
		return "redirect:/upput";
	}
	

	
	@RequestMapping("/detailGoods")
	public String detailGoods(Integer stock_id,Model model) {
		Stock stock = stockService.findGoodsById(stock_id);
		model.addAttribute("stock",stock);
		return "detail";
	}
	
	@RequestMapping("/detailGoods2")
	public String detailGoods2(Integer stock_id,Model model) {
		Stock stock2 = stockService.findGoodsById(stock_id);
		model.addAttribute("stock2",stock2);
		return "detail2";
	}
	
	@RequestMapping("/updateStock")
	public String updateStock(Integer stock_id,Model model) {
		Stock stock2=stockService.selectStockById(stock_id);
		model.addAttribute("stock2",stock2);
		return "updateStock";
	}
	@RequestMapping("/toupdateStock")
	public String updateStock(Stock stock) {
		/*String product=stock.getProduct();
		Float price = stock.getPrice();
		Integer s = stock.getS();
		Integer m = stock.getM();
		Integer l = stock.getL();
		Integer quantity = stock.getQuantity();
		String url =stock.getStatus();*/
		stockService.updateStock(stock);
		return "redirect:/updatee";
	}
	
	
	@RequestMapping("/toinsert")
	public String toinsert() {
		return "insert";
	}
	
	@RequestMapping("/insertStock")
	public String insertStock(Stock stock,HttpServletRequest request) {
		stockService.insertStock(stock);
		return "redirect:/allStock";
	}
	@RequestMapping("/deleteStock")
	public String delectCart(HttpServletRequest request, HttpServletResponse response) {
		String[] items = request.getParameterValues("stock_id");
		for (int i = 0; i < items.length; i++) {
				int stock_id = Integer.parseInt(items[i]);
				System.out.println(stock_id+"----------");
				stockService.deleteStockById(stock_id);
		}
		return "redirect:/allStock";
	}
	/*@RequestMapping("/toinsertStock")
	public String toinsertStock() {
		return "insertStock";
	}*/
	
	@RequestMapping("/upput")
	public String upput(Model model) {
		List<Stock> stock4 = stockService.findStockByAll();
		model.addAttribute("stock4",stock4);
		return "upput";
	}
	
	@RequestMapping("/updatee")
	public String updatee(Model model) {
		List<Stock> stock5 = stockService.findStockByAll();
		model.addAttribute("stock5",stock5);
		return "update";
	}
}
